<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */
 
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\UserClass as UserClassModel;
use app\admin\model\User as UserModel;
use app\admin\validate\UserClassValidate;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class UserClass extends BaseController

{
	/**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new UserClassModel();
        $this->userModel=new UserModel();
		$this->uid = get_login_admin('id');
    }
    public function learning_performance(){
            $classlist=Db::name('UserClass')->where('status=1')->column('bj_name,bj_code');
            View::assign('classlist',$classlist);
            $this->bj_code=Db::name('UserClass')->value('bj_code');
            $param =get_params();
            $where = [];
            $where['bj_code']=!empty($param['bj_code'])?$param['bj_code']:$this->bj_code;
            View::assign('bj_code',$where['bj_code']);
            if(request()->isAjax()){
                $st_list=Db::name('User')->where($where)->field('st_code,name,score,score1,score2,score3,score4,login_num')->select();
                $res['data'] = $st_list;                
                return table_assign(0, '', $res); 
            }else{
                return view();
            }

            
    }
    /**
     * 学生座位表
     */
    public function class_seats(){
        $classlist=Db::name('UserClass')->where('status=1')->column('bj_name,bj_code');
        View::assign('classlist',$classlist);
            $this->bj_code=Db::name('UserClass')->value('bj_code');
            $param =get_params();
            $where = [];
            $where['bj_code']=!empty($param['bj_code'])?$param['bj_code']:$this->bj_code;
            View::assign('bj_code',$where['bj_code']);
        if(request()->isAjax()){            
            $st_list=$this->userModel->getClassSeatsList($where,$param);         
            return table_assign(0, '', $st_list);
        }else{
            return view();
        }
        

    }
    /**
    * 数据列表
    */
    public function datalist()
    {
        $grades=[
            ['id'=>3, 'title'=>'3年级'],
            ['id'=>4, 'title'=>'4年级'],
            ['id'=>5, 'title'=>'5年级'],
            ['id'=>6, 'title'=>'6年级']
        ];
        View::assign('grades',$grades);
        if (request()->isAjax()) {
			$param = get_params();
			$where = [];
			if (!empty($param['keywords'])) {
                $where[] = ['headteacher|bj_name', 'like', '%' . $param['keywords'] . '%'];
            }
            if (!empty($param['grade_id'])) {
                $where[] = ['grade', '=', $param['grade_id']];
            }
            $list = $this->model->getUserClassList($where,$param);
            return table_assign(0, '', $list);
        }else{
            return view();
        }
    }

    /**
    * 添加
    */
    public function add()
    {
        if (request()->isAjax()) {		
			$param = get_params();
            $param['update_ip']=request()->ip();
			$param['create_time']=time();
            // 检验完整性
            try {
                validate(UserClassValidate::class)->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
			
            $this->model->addUserClass($param);
        }else{
			return view();
		}
    }
	

    /**
    * 编辑
    */
    public function edit()
    {
		$param = get_params();
		
        if (request()->isAjax()) {			
            // 检验完整性
            try {
                validate(UserClassValidate::class)->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
			
            $this->model->editUserClass($param);
        }else{
			$id = isset($param['id']) ? $param['id'] : 0;
			$detail = $this->model->getUserClassById($id);
			if (!empty($detail)) {
				View::assign('detail', $detail);
				return view();
			}
			else{
				throw new \think\exception\HttpException(404, '找不到页面');
			}			
		}
    }


    /**
    * 查看信息
    */
    public function read()
    {
        $param = get_params();
		$id = isset($param['id']) ? $param['id'] : 0;
		$detail = $this->model->getUserClassById($id);
		if (!empty($detail)) {
			View::assign('detail', $detail);
			return view();
		}
		else{
			throw new \think\exception\HttpException(404, '找不到页面');
		}
    }

    /**
    * 删除
	* type=0,逻辑删除，默认
	* type=1,物理删除
    */
    public function del()
    {
        $param = get_params();
		$id = isset($param['id']) ? $param['id'] : 0;
		$type = isset($param['type']) ? $param['type'] : 0;

        $this->model->delUserClassById($id,$type);
   }
}
